<?php

class YouNet_Form_ShareForm extends YouNet_Form {

    protected $_element_decorators = array(
        'ViewHelper', 'Description', 'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'dd')),
        array(array('label' => 'Label'), array('tag' => 'dt')),
        //array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'sharing-form-element'))
    );
    protected $_file_decorators = array(
        'File', 'Description', 'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'dd')),
        array(array('label' => 'Label'), array('tag' => 'dt')),
       // array(array('file' => 'HtmlTag'), array('tag' => 'div', 'class' => 'sharing-form-file'))
    );
    
    private $_sharing_image = NULL;
    private $_path_sharing_image = NULL;
    protected  $_id_campaign = NULL ;
    
    function __construct($id_campaign) {
        $this->_id_campaign = $id_campaign ;
        parent::__construct();
    }
    

    public function init() {
        /* Form */
        $this->setName('sharing_form');
        $this->setAttrib('class', 'sm_form classic');
                
        // Elements
        $this->_initTitle();
        $this->_initImage();
        $this->_initDescription();

        //add hidden field id_campaign
        $this->addElement('hidden','id_campaign',array(
            'value' => $this->_id_campaign
        ));
        $this->id_campaign->removeDecorator('Label');
        
        // Submit element
        $this->addElement('submit', 'submit', array(
            'label' => 'Submit'
        ));
        
        $this->submit->removeDecorator('DtDdWrapper');
    }

    // Init Title
    private function _initTitle() {
        $this->addElement('text', 'title', array(
            'decorators' => $this->_element_decorators,
            'label' => 'SHARING_TITLE',
            'filters' => array('StringTrim'),
            'required' => true
        ));
        $this->getElement('title')->addValidator('stringLength', false, array(0, 255));
    }

    // Init Image
    private function _initImage() {
        $this->addElement(new YouNet_Form_Element_File('image', array(
                    'decorators' => $this->_file_decorators,
                    'label' => 'SHARING_IMAGE',
                    'size' => 50
                )));
        $this->image->setDestination(realpath( FOLDER_SHARE));

        // limit to 100K
        $this->image->addValidator('Size', false, array(
            'max'   => '1MB'
        ));//204800 *5
        // only JPEG, PNG, and GIFs
        $this->image->addValidator('Extension', false, 'jpg,png,gif,jpeg');
        $this->image->setDescription(sprintf(
                        $this->getTranslator()->translate('UPLOAD_IMAGE_HINT'), '1MB', FANPAGE_MAX_WIDTH . 'px'
                ));
    }

    // Init Description
    private function _initDescription() {
        $this->addElement('textarea', 'description', array(
            'decorators' => $this->_element_decorators,
            'label' => 'SHARING_MESSAGE',
            'filters' => array('StringTrim'),
            'rows' => 7,
            'cols' => 60,
            'required' => true
        ));
    }

    public function addThumbnail() {
        $description = sprintf($this->getTranslator()->translate('UPLOAD_IMAGE_HINT'), '1 MB', FANPAGE_MAX_WIDTH . 'px');
//        if (!empty($image)) {
//            $path = STATIC_URL . '/upload/sharing/thumbnail/' . $image;
//            $description = '<span class="hint-text">' . $description . '</span>';
//            $description .= '<span class="hint-image">';
//            $description .= '<input type="hidden" name="is_delete_image"/>';
//            $description .= '<img id="id_image_thumbnail" src="' . $path . '"/>';
//            $description .= '</span>';
//            $description .= '<span class="btn_delete">Delete</span>';
//        } else {
//            $path = STATIC_URL . '/img/default-image.png';
//            $description = '<span class="hint-text">' . $description . '</span>';
//            $description .= '<span class="hint-image">';
//            $description .= '<input type="hidden" name="is_delete_image"/>';
//            $description .= '<img id="id_image_thumbnail" src="' . $path . '"/>';
//            $description .= '</span>';
//        }
        
        if (!$this->_sharing_image)
        {
           throw new Exception('Not set image logo.');
        }
        
        $path = $this->_sharing_image ;//STATIC_URL . '/img/default-image.png';
        
        $description = '<span class="hint-text">' . $description . '</span>';
        $description .= '<span class="hint-image">';
        $description .= '<input type="hidden" name="is_delete_image"/>';
        $description .= '<img id="id_image_thumbnail" src="' . $path . '"/>';
        $description .= '</span>';
        
        $this->image->setDescription($description);
        $this->image->getDecorator('Description')->setOption('escape', false);
    }

    public function getImage() {
        return $this->_getImageNameAndReceive();
    }

    private function _getImageNameAndReceive() {
        $ret_name = '';
        /* @var $imageElement Zend_Form_Element_File */
        $imageElement = $this->image;
        if (!empty($imageElement)) {
            $image_name = $imageElement->getFileName(null, false);
            if (!empty($image_name)) {
                //get image uploaded
                $max_file_name_length = MAX_FILENAME_LENGTH - 16;

                if (strlen($image_name) > $max_file_name_length) {
                    $image_name = substr($image_name, -$max_file_name_length);
                }
                $ret_name = time() . '_' . mt_rand(0, 9999) . '_' . preg_replace('/[^A-Za-z0-9-_.]/', '', $image_name);
                $target = FOLDER_SHARE;
                $target .= '/' . $ret_name;
                
                $imageElement->addFilter('Rename', $target);
                if ($imageElement->receive()) {
                }
            }
            else{
                 //get image default
                if ($this->_path_sharing_image)
                {
                    $ret_name = $this->makeDefaultImgToMainImg($this->_path_sharing_image);
                }
               
                
            }
        }
        return $ret_name;
    }

    protected function _cropImage($source, $target) {

        Core_Util::createScaledThumbnail($source, $target, DEFAULT_PRODUCT_THUMNAIL);
    }

    public function render(Zend_View_Interface $view = NULL) {
        $this->addThumbnail($this->_sharing_image);
        return parent::render($view);
    }

    public function setThumbnail($image = NULL) {
        $this->_sharing_image = $image;
    }
    public function seLabelSubmit($text)
    {
        $this->submit->setLabel($text);
    }
     public function setClassSubmit($text)
    {
        $this->submit->setAttrib("class",$text);
    }
    
    public function setDefaultImage($image)
    {
        $this->_sharing_image = STATIC_URL. $image;
        $this->_path_sharing_image = INDEX_PATH. $image;
    }
    
    public function makeDefaultImgToMainImg($defaultImg)
    {
        
        $file_name = basename($defaultImg) ;
        $target = FOLDER_SHARE .'/' . $file_name ;
        //echo "from image : " . $defaultImg ;
        //echo "\n to image : " . $target ;
        if(!copy($defaultImg,$target))
        {
            throw new Exception("Can not copy Image - makeDefaultImgToMainImg() ");
        }
        return $file_name ;
    }
}